package demo.hdz.testui;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.io.InputStream;

public class ImageTextActivity extends AppCompatActivity {

    public static final String TAG = "ImageTextActivity";
    private final String IMAGES_PATH = "images";

    private int mLineImgCount = 4;
    private int mImgWidth = 0;
    private int mImgHeight = 0;
    private int mImgMargin = 10;
    private int mImgRowMargin = 20;

    private int mImgCount = 0;

    private LinearLayout mMainLayout;
    private LinearLayout mRowLayout = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_text);

        init();

        Button btnBack = (Button) findViewById(R.id.btnBack);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mMainLayout = (LinearLayout)findViewById(R.id.linearLayout);

        addImages(getApplicationContext());
    }

    private void init() {
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        int height = outMetrics.heightPixels;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            DisplayMetrics metric = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getRealMetrics(metric);
            width = outMetrics.widthPixels;
            height = outMetrics.heightPixels;
        }
        if (width >= height) {
            mLineImgCount = 5;
            mImgMargin = 20;
            mImgRowMargin = 80;
        } else {
            mLineImgCount = 3;
            mImgMargin = 10;
            mImgRowMargin = 80;
        }
        mImgWidth = (width / mLineImgCount) - (mImgMargin * 2);
        Log.d(TAG, "init: mImgWidth = " + mImgWidth);
        mImgHeight = mImgWidth + 100;
    }

    private void addImages(Context context) {
        AssetManager am = getAssets();
        try {
            String[] tmpFileNames = am.list(IMAGES_PATH);
            for (String fileName : tmpFileNames) {
                Bitmap bitmap = getBitmapFromAssets(context, fileName);
                addImage(bitmap, fileName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //addOccupyImageView(mRowLayout, mImgCount);
    }

    private void addImage(Bitmap bitmap, String imageName) {
        if (bitmap == null) {
            return;
        }
        Log.d(ImageTextActivity.TAG, ""+bitmap.getWidth()+" " + bitmap.getHeight());
        if (mImgCount%mLineImgCount == 0) {
            addSpaceView(mRowLayout);
            mRowLayout = new LinearLayout(this);
            LinearLayout.LayoutParams rowLP = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            rowLP.setMargins(0, mImgRowMargin, 0, 0);
            mRowLayout.setLayoutParams(rowLP);
            mMainLayout.addView(mRowLayout);
            addSpaceView(mRowLayout);
        }
        mImgCount++;

        final ImageTextView imageView = new ImageTextView(this);
        imageView.setImageBitmap(bitmap);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(mImgWidth, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.TOP;
        lp.setMargins(mImgMargin, 0, mImgMargin, 0);
        imageView.setLayoutParams(lp);
        imageView.setText(imageName);
        imageView.setTextPaddingTop(20);
        imageView.setTextSize(20);
        imageView.setTextColor(Color.WHITE);
        imageView.setImageSize(mImgWidth-100, mImgWidth-100);

        if (mRowLayout != null) {
            mRowLayout.addView(imageView);
            onClickImageView(imageView, imageName);
        }
    }

    private void onClickImageView(final ImageTextView imageView, final String imageName) {
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ImageTextActivity.this, imageName, Toast.LENGTH_SHORT).show();
            }
        });
    }
    private void addOccupyImageView(LinearLayout rowLL, int count) {
        if (rowLL == null) {
            return;
        }
        int j = mLineImgCount - count%mLineImgCount;
        if (j != mLineImgCount) {
            for (int i = 0; i < j; i++) {
                ImageView imageView0 = new ImageView(this);
                LinearLayout.LayoutParams lp0 = new LinearLayout.LayoutParams(mImgWidth, mImgHeight);
                lp0.gravity = Gravity.CENTER;
                lp0.setMargins(mImgMargin, 0, mImgMargin, 0);
                imageView0.setLayoutParams(lp0);
                rowLL.addView(imageView0, count%mLineImgCount+1);
            }
        }
        addSpaceView(rowLL);
    }
    private void addSpaceView(LinearLayout rowLL) {
/*        if (rowLL != null) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.weight = 1.0f;
            View view = new View(this);
            view.setLayoutParams(params);
            rowLL.addView(view);
        }*/
    }
    public Bitmap getBitmapFromAssets(Context context, String fileName){
        Bitmap bitmap = null;
        AssetManager am = context.getAssets();
        try{
            InputStream is = am.open(IMAGES_PATH + "/" + fileName);
            bitmap = BitmapFactory.decodeStream(is);
        }catch(Exception e){
            e.printStackTrace();
        }
        return bitmap;
    }
}
